﻿using UnityEngine;
using System.Collections.Generic;

public delegate void NetworkEvent(ByteBuffer buffer);

public class NetworkManager : MonoBehaviour {

    private static NetworkManager _instance;
    public static NetworkManager Instance
    {
        get
        {
            return _instance;
        }
    }

    SocketClient client;

    //存储子线程的消息体
    Queue<ByteBuffer> queue = new Queue<ByteBuffer>();
    public event NetworkEvent networkEvent;

    private void Awake()
    {
        _instance = this;

        client = new SocketClient();
    }
	
    public void AddEnvent(ByteBuffer buffer)
    {
        queue.Enqueue(buffer);
    }

    private void Update()
    {
        if (queue.Count > 0)
        {
            ByteBuffer buffer = queue.Dequeue();

            if(networkEvent!=null)
            {
                networkEvent(buffer);
            }
        }
    }

    //ui调用的发送服务器的方法
    public void SendMessage(int protocol,string json)
    {
        client.SendMessage(protocol,json);
    }
}
